package com.samphanie.ane.library.log;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.samphanie.ane.library.R;

import java.util.ArrayList;
import java.util.List;

/**
 * AneViewPrinter 将log显示在界面上
 * @author ZSY
 */
public class AneViewPrinter implements AneLogPrinter {

    private RecyclerView recyclerView;
    private LogAdapter logAdapter;

    private AneViewPrinterProvider viewPrinterProvider;

    public AneViewPrinter(Activity activity) {
        FrameLayout rootView = activity.findViewById(android.R.id.content);
        recyclerView = new RecyclerView(activity);
        logAdapter = new LogAdapter(LayoutInflater.from(recyclerView.getContext()));
        LinearLayoutManager layoutManager = new LinearLayoutManager(recyclerView.getContext());
        recyclerView.setAdapter(logAdapter);
        recyclerView.setLayoutManager(layoutManager);

        viewPrinterProvider = new AneViewPrinterProvider(rootView, recyclerView);
    }

    /**
     * 获取ViewProvider，通过ViewProvider可以控制log视图的展示和隐藏
     *
     * @return ViewProvider
     */
    @NonNull
    public AneViewPrinterProvider getViewProvider() {
        return viewPrinterProvider;
    }

    @Override
    public void print(@NonNull AneLogConfig config, int level, String tag, @NonNull String printString) {
        // 将log展示添加到recycleView
        logAdapter.addItem(new AneLogMo(System.currentTimeMillis(), level, tag, printString));
        // 滚动到对应的位置
        recyclerView.smoothScrollToPosition(logAdapter.getItemCount() - 1);
    }

    private static class LogAdapter extends RecyclerView.Adapter<LogAdapter.LogViewHolder> {

        private LayoutInflater inflater;
        private List<AneLogMo> logs = new ArrayList<>();

        public LogAdapter(LayoutInflater inflater) {
            this.inflater = inflater;
        }

        void addItem(AneLogMo logItem) {
            logs.add(logItem);
            notifyItemInserted(logs.size() - 1);
        }

        @NonNull
        @Override
        public LogViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            // 加载布局
            View itemView = inflater.inflate(R.layout.anelog_item, parent, false);
            return new LogViewHolder(itemView);
        }

        @Override
        public void onBindViewHolder(@NonNull LogViewHolder holder, int position) {
            AneLogMo logItem = logs.get(position);
            int color = getHighlightColor(logItem.level);
            holder.tagView.setTextColor(color);
            holder.messageView.setTextColor(color);

            holder.tagView.setText(logItem.getFlattened());
            holder.messageView.setText(logItem.log);

        }

        /**
         * 根据log 级别获取不同的高亮颜色
         *
         * @param  logLevel log级别
         * @return 高亮的颜色
         */
        private int getHighlightColor(int logLevel) {
            int highlight;
            switch (logLevel) {
                case AneLogType.V:
                    highlight = 0xffbbbbbb;
                    break;
                case AneLogType.D:
                    highlight = 0xffffffff;
                    break;
                case AneLogType.I:
                    highlight = 0xff6a8759;
                    break;
                case AneLogType.W:
                    highlight = 0xffbbb529;
                    break;
                case AneLogType.E:
                    highlight = 0xffff6b68;
                    break;
                default:
                    highlight = 0xffffff00;
                    break;
            }

            return highlight;
        }

        @Override
        public int getItemCount() {
            return logs.size();
        }

        private static class LogViewHolder extends RecyclerView.ViewHolder {

            TextView tagView;
            TextView messageView;

            public LogViewHolder(@NonNull View itemView) {
                super(itemView);
                tagView = itemView.findViewById(R.id.tag);
                messageView = itemView.findViewById(R.id.message);
            }
        }
    }

}
